package com.ttl.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttl.web.entity.Banner;
import com.ttl.web.entity.PageParam;
import com.ttl.web.service.BannerService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.ttl.web.utils.ResultVo;

import java.util.List;
import java.util.Optional;

/**
 * 系统轮播图操作接口
 **/
@RestController
@RequestMapping("/api/banner")
public class BannerController {

    @Resource
    private BannerService bannerService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public ResultVo add(@RequestBody Banner banner) {
        if (bannerService.save(banner)) {
            return ResultVo.success("新增成功");
        }else {
            return ResultVo.error("新增失败");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ResultVo deleteById(@PathVariable Integer id) {
        if (bannerService.removeById(id)){
            return ResultVo.success("删除成功");
        }else {
            return ResultVo.error("删除失败");
        }
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public ResultVo updateById(@RequestBody Banner banner) {
        if (bannerService.updateById(banner)) {
            return ResultVo.success("修改成功");
        }else {
            return ResultVo.error("修改失败");
        }
    }
    /**
     * 获取首页轮播图
     */
    @GetMapping("/getIndexBanner")
    public ResultVo getIndexBanner() {
        List<Banner> list = bannerService.list();
        return ResultVo.success(list);
    }

    /**
     * 查询最新的
     */
    @GetMapping("/selectNewest")
    public ResultVo selectNewest() {
        List<Banner> banners = bannerService.list(new LambdaQueryWrapper<Banner>().orderByDesc(Banner::getOrderNum).last("LIMIT 1"));
        if (!banners.isEmpty()) {
            return ResultVo.success(banners.get(0));
        }
        return ResultVo.error("没有找到公告");
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public ResultVo selectById(@PathVariable Integer id) {
        Banner banner = bannerService.getById(id);
        return ResultVo.success(banner);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public ResultVo selectAll() {
        List<Banner> list = bannerService.list();
        return ResultVo.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping
    public ResultVo getList(PageParam param) {
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getName())) {
            queryWrapper.like(Banner::getTitle, param.getName());
        }
        // 创建分页对象
        Page<Banner> page = new Page<>(Optional.ofNullable(param.getCurrentPage()).orElse(1), param.getPageSize());
        // 执行分页查询
        IPage<Banner> pageList = bannerService.page(page, queryWrapper);
        // 返回结果
        return ResultVo.success(pageList);
    }

}